package biz

import (
	"context"
	pb "manage/api/course"

	"github.com/go-kratos/kratos/v2/log"
)

func NewCourseUse(repo CourseRepo, logger log.Logger) *CourseCase {
	return &CourseCase{repo: repo, log: log.NewHelper(logger)}
}

func (c *CourseCase) Create(ctx context.Context, req *pb.CreateCourseRequest) (*pb.CreateCourseResponse, error) {

	// 初始化
	reply := &pb.CreateCourseResponse{Code: 200, Msg: "考试创建成功"}
	// 构造
	resp, err := c.repo.CreateCourse(&CreateCourseRequest{
		CourseName:        req.CourseName,
		CourseDescription: req.CourseDescription,
	})
	if err != nil {
		reply.Code = 400
		reply.Msg = err.Error()
	} else {
		reply.CourseId = resp.CourseID
	}
	return reply, nil
}
func (c *CourseCase) Delete(ctx context.Context, req *pb.DeleteCourseRequest) (*pb.DeleteCourseResponse, error) {

	// 初始化
	reply := &pb.DeleteCourseResponse{Code: 200, Msg: "考试删除成功"}
	// 构造
	err := c.repo.DeleteCourse(req.CourseId)
	if err != nil {
		reply.Code = 400
		reply.Msg = err.Error()
	}
	return reply, nil
}
func (c *CourseCase) Update(ctx context.Context, req *pb.UpdateCourseRequest) (*pb.UpdateCourseResponse, error) {

	// 初始化
	reply := &pb.UpdateCourseResponse{Code: 200, Msg: "考试更新成功"}
	// 构造
	err := c.repo.UpdateCourse(&CreateCourseRequest{
		CourseName:        req.CourseName,
		CourseDescription: req.CourseDescription,
	}, req.CourseId)
	if err != nil {
		reply.Code = 400
		reply.Msg = err.Error()
	}
	return reply, nil
}
func (c *CourseCase) Get(ctx context.Context, req *pb.GetCourseRequest) (*pb.GetCourseResponse, error) {

	// 初始化
	reply := &pb.GetCourseResponse{Code: 200, Msg: "查询成功"}
	// 构造
	resp, err := c.repo.GetCourseInfo(req.CourseId)
	if err != nil {
		reply.Code = 400
		reply.Msg = err.Error()
	} else {
		reply.CourseInfo = &pb.CourseInfo{
			CourseId:          resp.CourseID,
			CourseName:        resp.CourseName,
			CourseDescription: resp.CourseDescription,
		}
	}
	return reply, nil
}
